package com.example.myanimator.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @author tgw
 * @date 2021/3/16
 * @describe
 */
public class DrawCanvas extends View {

    private Paint mPaint;
    private Path mPath;
    private Canvas mCanvas;


    public DrawCanvas(Context context) {
        super(context);
    }

    public DrawCanvas(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setStrokeWidth(5);//设置笔画宽度
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setTextAlign(Paint.Align.CENTER);//设置文字对齐方式
        mPaint.setColor(Color.WHITE);//设置文字对齐方式
        mPath = new Path();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
       int c1= canvas.save();//保存当前画布大小，即整屏
        //裁剪 画布,使画布 只能这么大
        canvas.clipRect(new RectF(100,0,200,100));
        canvas.drawColor(Color.RED);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(new RectF(0,0,500,100),mPaint);
        int c2= canvas.save();
        canvas.clipRect(new RectF(180,0,500,100));
        canvas.drawColor(Color.YELLOW);
        int c3= canvas.save();
        //恢复画布大小
//        canvas.restore();//恢复至上一级
//        canvas.restore();
        canvas.restoreToCount(c3);//恢复至指定画布
        canvas.drawColor(Color.BLUE);
    }


}
